/*
 * @文件描述: 
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-08-17 10:28:41
 */
#include "TFT.h"
#ifdef WIN_SIM
/*
*********************************************************************************************************
*                                             uC/GUI V3.98
*                        Universal graphic software for embedded applications
*
*                       (c) Copyright 2002, Micrium Inc., Weston, FL
*                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
*              �C/GUI is protected by international copyright laws. Knowledge of the
*              source code may not be used to write a similar product. This file may
*              only be used in accordance with a license and should not be redistributed
*              in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File        : LCDTemplate.c
Purpose     : Empty driver
              This driver does no perform any function, but it can be
              used for 2 purposes:
              a) Satisfy all externals so an application can be
                compiled and linked in target hardware even if the
                driver is not already available
              b) Template for a starting point for a new driver.
----------------------------------------------------------------------   
Adapting to a new system (creating a new driver):
  In this case, the first step is to fill the routines 
  LCD_L0_GetPixelIndex, LCD_L0_SetPixelIndex and LCD_L0_Init with
  functionality, which is sufficient to make the hardware work.
  A second (optional) step would be to optimize higher level routines. 
----------------------------------------------------------------------   
Version-Date---Author-Explanation                                        
----------------------------------------------------------------------   
1.00.00 020417 JE     a) Changed to have only to adapt _GetPixelIndex
                         and _SetPixelIndex
0.90.00 020214 JE     a) First release
---------------------------END-OF-HEADER------------------------------
*/

#include <stddef.h>
#include "LCD_Private.h" /* private modul definitions & config */
#include "GUI_Private.h"
#include "GUIDebug.h"
#include <math.h>
#include "TFT.h"
// #if (LCD_CONTROLLER == -1) \
//     && (!defined(WIN32) | defined(LCD_SIMCONTROLLER))
#if (LCD_CONTROLLER == -1)
/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

#ifndef LCD_INIT_CONTROLLER
#define LCD_INIT_CONTROLLER()
#endif
#ifndef LCD_SET_ORG
#define LCD_SET_ORG(x, y) \
  GUI_USE_PARA(x);        \
  GUI_USE_PARA(y)
#endif

/*********************************************************************
*
*       Macros for MIRROR_, SWAP_ and LUT_
*/
#if (!defined(LCD_LUT_COM) && !defined(LCD_LUT_SEG))
#if (!LCD_MIRROR_X && !LCD_MIRROR_Y && !LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) x
#define LOG2PHYS_Y(x, y) y
#elif (!LCD_MIRROR_X && !LCD_MIRROR_Y && LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) y
#define LOG2PHYS_Y(x, y) x
#elif (!LCD_MIRROR_X && LCD_MIRROR_Y && !LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) x
#define LOG2PHYS_Y(x, y) LCD_YSIZE - 1 - (y)
#elif (!LCD_MIRROR_X && LCD_MIRROR_Y && LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) y
#define LOG2PHYS_Y(x, y) LCD_XSIZE - 1 - (x)
#elif (LCD_MIRROR_X && !LCD_MIRROR_Y && !LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) LCD_XSIZE - 1 - (x)
#define LOG2PHYS_Y(x, y) y
#elif (LCD_MIRROR_X && !LCD_MIRROR_Y && LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) LCD_YSIZE - 1 - (y)
#define LOG2PHYS_Y(x, y) x
#elif (LCD_MIRROR_X && LCD_MIRROR_Y && !LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) LCD_XSIZE - 1 - (x)
#define LOG2PHYS_Y(x, y) LCD_YSIZE - 1 - (y)
#elif (LCD_MIRROR_X && LCD_MIRROR_Y && LCD_SWAP_XY)
#define LOG2PHYS_X(x, y) LCD_YSIZE - 1 - (y)
#define LOG2PHYS_Y(x, y) LCD_XSIZE - 1 - (x)
#endif
#else
#if (defined(LCD_LUT_COM) && !defined(LCD_LUT_SEG))
#define LOG2PHYS_X(x, y) x
#define LOG2PHYS_Y(x, y) LCD__aLine2Com0[y]
#elif (!defined(LCD_LUT_COM) && defined(LCD_LUT_SEG))
#define LOG2PHYS_X(x, y) LCD__aCol2Seg0[x]
#define LOG2PHYS_Y(x, y) y
#elif (defined(LCD_LUT_COM) && defined(LCD_LUT_SEG))
#define LOG2PHYS_X(x, y) LCD__aCol2Seg0[x]
#define LOG2PHYS_Y(x, y) LCD__aLine2Com0[y]
#endif
#endif

/*********************************************************************
*
*       Static functions
*
**********************************************************************
*/

/*********************************************************************
*
*       Draw Bitmap 1 BPP
*/
static void _DrawBitLine1BPP(int x, int y, U8 const GUI_UNI_PTR *p, int Diff, int xsize, const LCD_PIXELINDEX *pTrans)
{
  LCD_PIXELINDEX Index0 = *(pTrans + 0);
  LCD_PIXELINDEX Index1 = *(pTrans + 1);
  x += Diff;
  switch (GUI_Context.DrawMode & (LCD_DRAWMODE_TRANS | LCD_DRAWMODE_XOR))
  {
  case 0:
    do
    {
      LCD_L0_SetPixelIndex(x++, y, (*p & (0x80 >> Diff)) ? Index1 : Index0);
      if (++Diff == 8)
      {
        Diff = 0;
        p++;
      }
    } while (--xsize);
    break;
  case LCD_DRAWMODE_TRANS:
    do
    {
      if (*p & (0x80 >> Diff))
        LCD_L0_SetPixelIndex(x, y, Index1);
      x++;
      if (++Diff == 8)
      {
        Diff = 0;
        p++;
      }
    } while (--xsize);
    break;
  case LCD_DRAWMODE_XOR | LCD_DRAWMODE_TRANS:
  case LCD_DRAWMODE_XOR:
    do
    {
      if (*p & (0x80 >> Diff))
      {
        int Pixel = LCD_L0_GetPixelIndex(x, y);
        LCD_L0_SetPixelIndex(x, y, LCD_NUM_COLORS - 1 - Pixel);
      }
      x++;
      if (++Diff == 8)
      {
        Diff = 0;
        p++;
      }
    } while (--xsize);
    break;
  }
}

/*********************************************************************
*
*       Draw Bitmap 2 BPP
*/
#if (LCD_MAX_LOG_COLORS > 2)
static void _DrawBitLine2BPP(int x, int y, U8 const GUI_UNI_PTR *p, int Diff, int xsize, const LCD_PIXELINDEX *pTrans)
{
  LCD_PIXELINDEX Pixels = *p;
  int CurrentPixel = Diff;
  x += Diff;
  switch (GUI_Context.DrawMode & (LCD_DRAWMODE_TRANS | LCD_DRAWMODE_XOR))
  {
  case 0:
    if (pTrans)
    {
      do
      {
        int Shift = (3 - CurrentPixel) << 1;
        int Index = (Pixels & (0xC0 >> (6 - Shift))) >> Shift;
        LCD_PIXELINDEX PixelIndex = *(pTrans + Index);
        LCD_L0_SetPixelIndex(x++, y, PixelIndex);
        if (++CurrentPixel == 4)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    else
    {
      do
      {
        int Shift = (3 - CurrentPixel) << 1;
        int Index = (Pixels & (0xC0 >> (6 - Shift))) >> Shift;
        LCD_L0_SetPixelIndex(x++, y, Index);
        if (++CurrentPixel == 4)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    break;
  case LCD_DRAWMODE_TRANS:
    if (pTrans)
    {
      do
      {
        int Shift = (3 - CurrentPixel) << 1;
        int Index = (Pixels & (0xC0 >> (6 - Shift))) >> Shift;
        if (Index)
        {
          LCD_PIXELINDEX PixelIndex = *(pTrans + Index);
          LCD_L0_SetPixelIndex(x, y, PixelIndex);
        }
        x++;
        if (++CurrentPixel == 4)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    else
    {
      do
      {
        int Shift = (3 - CurrentPixel) << 1;
        int Index = (Pixels & (0xC0 >> (6 - Shift))) >> Shift;
        if (Index)
        {
          LCD_L0_SetPixelIndex(x, y, Index);
        }
        x++;
        if (++CurrentPixel == 4)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    break;
  }
}
#endif

/*********************************************************************
*
*       Draw Bitmap 4 BPP
*/
#if (LCD_MAX_LOG_COLORS > 4)
static void _DrawBitLine4BPP(int x, int y, U8 const GUI_UNI_PTR *p, int Diff, int xsize, const LCD_PIXELINDEX *pTrans)
{
  LCD_PIXELINDEX Pixels = *p;
  int CurrentPixel = Diff;
  x += Diff;
  switch (GUI_Context.DrawMode & (LCD_DRAWMODE_TRANS | LCD_DRAWMODE_XOR))
  {
  case 0:
    if (pTrans)
    {
      do
      {
        int Shift = (1 - CurrentPixel) << 2;
        int Index = (Pixels & (0xF0 >> (4 - Shift))) >> Shift;
        LCD_PIXELINDEX PixelIndex = *(pTrans + Index);
        LCD_L0_SetPixelIndex(x++, y, PixelIndex);
        if (++CurrentPixel == 2)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    else
    {
      do
      {
        int Shift = (1 - CurrentPixel) << 2;
        int Index = (Pixels & (0xF0 >> (4 - Shift))) >> Shift;
        LCD_L0_SetPixelIndex(x++, y, Index);
        if (++CurrentPixel == 2)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    break;
  case LCD_DRAWMODE_TRANS:
    if (pTrans)
    {
      do
      {
        int Shift = (1 - CurrentPixel) << 2;
        int Index = (Pixels & (0xF0 >> (4 - Shift))) >> Shift;
        if (Index)
        {
          LCD_PIXELINDEX PixelIndex = *(pTrans + Index);
          LCD_L0_SetPixelIndex(x, y, PixelIndex);
        }
        x++;
        if (++CurrentPixel == 2)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    else
    {
      do
      {
        int Shift = (1 - CurrentPixel) << 2;
        int Index = (Pixels & (0xF0 >> (4 - Shift))) >> Shift;
        if (Index)
        {
          LCD_L0_SetPixelIndex(x, y, Index);
        }
        x++;
        if (++CurrentPixel == 2)
        {
          CurrentPixel = 0;
          Pixels = *(++p);
        }
      } while (--xsize);
    }
    break;
  }
}
#endif

/*********************************************************************
*
*       Draw Bitmap 8 BPP
*/
#if (LCD_MAX_LOG_COLORS > 16)
static void _DrawBitLine8BPP(int x, int y, U8 const GUI_UNI_PTR *p, int xsize, const LCD_PIXELINDEX *pTrans)
{
  LCD_PIXELINDEX Pixel;
  switch (GUI_Context.DrawMode & (LCD_DRAWMODE_TRANS | LCD_DRAWMODE_XOR))
  {
  case 0:
    if (pTrans)
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        Pixel = *p;
        LCD_L0_SetPixelIndex(x, y, *(pTrans + Pixel));
      }
    }
    else
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        LCD_L0_SetPixelIndex(x, y, *p);
      }
    }
    break;
  case LCD_DRAWMODE_TRANS:
    if (pTrans)
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        Pixel = *p;
        if (Pixel)
        {
          LCD_L0_SetPixelIndex(x, y, *(pTrans + Pixel));
        }
      }
    }
    else
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        Pixel = *p;
        if (Pixel)
        {
          LCD_L0_SetPixelIndex(x, y, Pixel);
        }
      }
    }
    break;
  }
}
#endif

/*********************************************************************
*
*       Draw Bitmap 16 BPP
*/
#if (LCD_BITSPERPIXEL > 8)
static void DrawBitLine16BPP(int x, int y, U16 const GUI_UNI_PTR *p, int xsize, const LCD_PIXELINDEX *pTrans)
{
  LCD_PIXELINDEX pixel;
  if ((GUI_Context.DrawMode & LCD_DRAWMODE_TRANS) == 0)
  {
    if (pTrans)
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        pixel = *p;
        LCD_L0_SetPixelIndex(x, y, *(pTrans + pixel));
      }
    }
    else
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        LCD_L0_SetPixelIndex(x, y, *p);
      }
    }
  }
  else
  {
    if (pTrans)
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        pixel = *p;
        if (pixel)
        {
          LCD_L0_SetPixelIndex(x, y, *(pTrans + pixel));
        }
      }
    }
    else
    {
      for (; xsize > 0; xsize--, x++, p++)
      {
        pixel = *p;
        if (pixel)
        {
          LCD_L0_SetPixelIndex(x, y, pixel);
        }
      }
    }
  }
}
#endif

/*********************************************************************
*
*       Exported functions
*
**********************************************************************
*/

/*********************************************************************
*
*       LCD_L0_SetPixelIndex
*
* Purpose:
*   Sets the index of the given pixel. The upper layers
*   calling this routine make sure that the coordinates are in range, so
*   that no check on the parameters needs to be performed.
*/
void LCD_L0_SetPixelIndex(int x, int y, int PixelIndex)
{
#if LCD_SWAP_XY | LCD_MIRROR_X | LCD_MIRROR_Y
  int xPhys = LOG2PHYS_X(x, y);
  int yPhys = LOG2PHYS_Y(x, y);
#else
#define xPhys x
#define yPhys y
#endif
  /* Write into hardware ... Adapt to your system */
  {
    TFT_SetPixel(xPhys, yPhys, PixelIndex);
    //TFT_SetTextColor(PixelIndex);
    //TFT_SetPointPixel(xPhys, yPhys);
  }
}

/*********************************************************************
*
*       LCD_L0_GetPixelIndex
*
* Purpose:
*   Returns the index of the given pixel. The upper layers
*   calling this routine make sure that the coordinates are in range, so
*   that no check on the parameters needs to be performed.
*/
unsigned int LCD_L0_GetPixelIndex(int x, int y)
{
/* Convert logical into physical coordinates (Dep. on LCDConf.h) */
#if LCD_SWAP_XY | LCD_MIRROR_X | LCD_MIRROR_Y
  int xPhys = LOG2PHYS_X(x, y);
  int yPhys = LOG2PHYS_Y(x, y);
#else
#define xPhys x
#define yPhys y
#endif
  /* Read from hardware ... Adapt to your system */
  {
    return TFT_GetPointPixel(xPhys, yPhys);
  }
}

/*********************************************************************
*
*       LCD_L0_XorPixel
*/
void LCD_L0_XorPixel(int x, int y)
{
  LCD_PIXELINDEX PixelIndex = LCD_L0_GetPixelIndex(x, y);
  LCD_L0_SetPixelIndex(x, y, LCD_NUM_COLORS - PixelIndex - 1);
}

/*********************************************************************
*
*       LCD_L0_DrawHLine
*/
void LCD_L0_DrawHLine(int x0, int y, int x1)
{

/* Convert logical into physical coordinates (Dep. on LCDConf.h) */
#if LCD_SWAP_XY | LCD_MIRROR_X | LCD_MIRROR_Y
  int x0Phys = LOG2PHYS_X(x0, y);
  int y0Phys = LOG2PHYS_Y(x0, y);

  int x1Phys = LOG2PHYS_X(x1, y);
  int y1Phys = LOG2PHYS_Y(x1, y);

#else
#define x0Phys x0
#define y0Phys y
#define x1Phys x1
#define y1Phys y
#endif

  if (GUI_Context.DrawMode & LCD_DRAWMODE_XOR)
  {
    for (; x0 <= x1; x0++)
    {
      LCD_L0_XorPixel(x0, y);
    }
  }
  else
  {

    // TFT_SetTextColor(LCD_COLORINDEX);
    //TFT_DrawLine(x0Phys, y0Phys, x1Phys, y1Phys);
    //SDL_Draw_H_Line(x0, y, x1, LCD_COLORINDEX);
    //原来的方法
    for (; x0 <= x1; x0++)
    {
      LCD_L0_SetPixelIndex(x0, y, LCD_COLORINDEX);
    }
  }
}

/*********************************************************************
*
*       LCD_L0_DrawVLine
*/
void LCD_L0_DrawVLine(int x, int y0, int y1)
{
  /* Convert logical into physical coordinates (Dep. on LCDConf.h) */
#if LCD_SWAP_XY | LCD_MIRROR_X | LCD_MIRROR_Y
  int x0Phys = LOG2PHYS_X(x, y0);
  int y0Phys = LOG2PHYS_Y(x, y0);

  int x1Phys = LOG2PHYS_X(x, y1);
  int y1Phys = LOG2PHYS_Y(x, y1);

#else
#define x0Phys x
#define y0Phys y0
#define x1Phys x
#define y1Phys y1
#endif
  if (GUI_Context.DrawMode & LCD_DRAWMODE_XOR)
  {
    for (; y0 <= y1; y0++)
    {
      LCD_L0_XorPixel(x, y0);
    }
  }
  else
  {
    // TFT_SetTextColor(LCD_COLORINDEX);
    // TFT_DrawLine(x0Phys, y0Phys, x1Phys, y1Phys);

    //原来的方法
    for (; y0 <= y1; y0++)
    {
      LCD_L0_SetPixelIndex(x, y0, LCD_COLORINDEX);
    }
  }
}

/*********************************************************************
*
*       LCD_L0_FillRect
*/
void LCD_L0_FillRect(int x0, int y0, int x1, int y1)
{

/* Convert logical into physical coordinates (Dep. on LCDConf.h) */
#if LCD_SWAP_XY | LCD_MIRROR_X | LCD_MIRROR_Y
  int x0Phys = LOG2PHYS_X(x0, y0);
  int y0Phys = LOG2PHYS_Y(x0, y0);

  int x1Phys = LOG2PHYS_X(x1, y1);
  int y1Phys = LOG2PHYS_Y(x1, y1);

#else
#define x0Phys x0
#define y0Phys y0
#define x1Phys x1
#define y1Phys y0
#endif

  // if (GUI_Context.DrawMode & LCD_DRAWMODE_XOR)
  // {
  //   LCD_L0_DrawHLine(x0, y0, x1);
  // }
  // else
  // {
  // //TFT_Clear2(x0Phys, y0Phys, x1Phys, y1Phys, LCD_COLORINDEX);
  // }
  //原来的方法：
  for (; y0 <= y1; y0++)
  {
    LCD_L0_DrawHLine(x0, y0, x1);
  }
}
//#define PERFORMANCE_MODE
/*********************************************************************
*
*       LCD_L0_DrawBitmap
*/
void LCD_L0_DrawBitmap(int x0, int y0,
                       int xsize, int ysize,
                       int BitsPerPixel,
                       int BytesPerLine,
                       const U8 GUI_UNI_PTR *pData, int Diff,
                       const LCD_PIXELINDEX *pTrans)
{
/* Convert logical into physical coordinates (Dep. on LCDConf.h) */
#if LCD_SWAP_XY | LCD_MIRROR_X | LCD_MIRROR_Y
  int x0Phys = LOG2PHYS_X(x0, y0);
  int y0Phys = LOG2PHYS_Y(x0, y0);
#else
#define x0Phys x0
#define y0Phys y0
#endif

#ifdef PERFORMANCE_MODE

#if LCD_SWAP_XY & LCD_MIRROR_X //正向横屏

  TFT_DrawBitmap(x0Phys, y0Phys, ysize, xsize, (uint16_t *)pData);
#else
  TFT_DrawBitmap(x0, y0, xsize, ysize, (uint16_t *)pData);
#endif

#else
  int i;

  /* Use _DrawBitLineXBPP */
  for (i = 0; i < ysize; i++)
  {
    switch (BitsPerPixel)
    {
    case 1:
      _DrawBitLine1BPP(x0, i + y0, pData, Diff, xsize, pTrans);
      break;
#if (LCD_MAX_LOG_COLORS > 2)
    case 2:
      _DrawBitLine2BPP(x0, i + y0, pData, Diff, xsize, pTrans);
      break;
#endif
#if (LCD_MAX_LOG_COLORS > 4)
    case 4:
      _DrawBitLine4BPP(x0, i + y0, pData, Diff, xsize, pTrans);
      break;
#endif
#if (LCD_MAX_LOG_COLORS > 16)
    case 8:
      _DrawBitLine8BPP(x0, i + y0, pData, xsize, pTrans);
      break;
#endif
#if (LCD_BITSPERPIXEL > 8)
    case 16:
      //原版：
      DrawBitLine16BPP(x0, i + y0, (const U16 *)pData, xsize, pTrans);

      //优化版：
      /* Convert logical into physical coordinates (Dep. on LCDConf.h) */

      //竖屏
      //TFT_DrawBitmap(x0Phys, y0Phys + i, xsize, 1, (uint16_t *)pData);
      //横屏
      //TFT_DrawBitmap(x0Phys - i, y0Phys, 1, xsize, (uint16_t *)pData);

      break;
#endif
    }
    pData += BytesPerLine;
  }
#endif
}

/*********************************************************************
*
*       LCD_L0_SetOrg
*/
void LCD_L0_SetOrg(int x, int y)
{
  LCD_SET_ORG(x, y);
}

/*********************************************************************
*
*       LCD_On / LCD_Off
*/
void LCD_On(void)
{
#ifdef LCD_ON
  LCD_ON();
#endif
}

void LCD_Off(void)
{
#ifdef LCD_OFF
  LCD_OFF();
#endif
}

/*********************************************************************
*
*       LCD_L0_Init
*
* Purpose:
*   Initialises the LCD-controller.
*/
int LCD_L0_Init(void)
{
  //LCD_INIT_CONTROLLER();
  return 0;
}

/*********************************************************************
*
*       LCD_L0_SetLUTEntry
*/
void LCD_L0_SetLUTEntry(U8 Pos, LCD_COLOR Color)
{
  GUI_USE_PARA(Pos);
  GUI_USE_PARA(Color);
}

/*********************************************************************
*
*       LCD_L0_GetDevFunc
*/
void *LCD_L0_GetDevFunc(int Index)
{
  GUI_USE_PARA(Index);
  return NULL;
}

#else

void LCDTemplate_c(void);
void LCDTemplate_c(void) {} /* avoid empty object files */

#endif /* (LCD_CONTROLLER undefined) */

#endif // WIN_SIM